Skip to content

Add MqttManager for broker connection lifecycle (CATROID-1671)#5222

Open
Paras-ydv wants to merge 9 commits into
Catrobat:developfrom
Paras-ydv:CATROID-1671
Open

Add MqttManager for broker connection lifecycle (CATROID-1671)#5222
Paras-ydv wants to merge 9 commits into
Catrobat:developfrom
Paras-ydv:CATROID-1671

Conversation

@Paras-ydv

Copy link
Copy Markdown

Summary

This PR implements the centralized MQTT Manager for Catroid, responsible for the full connection lifecycle of an MQTT broker client:

  • CATROID-1671: MqttManager — connect, disconnect, state tracking, and failure handling

⚠️ Dependency notice: This PR depends on the MQTT settings PR covering CATROID-1670 and CATROID-1672 (mqtt-settings-entry-task-tdd). The dependency commits have been cherry-picked here to keep this branch buildable independently. This PR should only be merged after that PR is merged. Once it is, this branch will be rebased and the cherry-picked commits removed.

This ticket is scoped to connection management only. Message publishing, subscriptions, and MQTT bricks are deferred to separate tickets.


CATROID-1671 — MqttManager for Broker Connection Lifecycle

Changes Implemented

org.catrobat.catroid.devices.mqtt

  • MqttClientInterface
    Abstracts the Paho client behind an interface, decoupling MqttManager from the third-party library and enabling pure JVM unit tests without an Android runtime or real network.

  • PahoMqttClient
    Thin adapter wrapping org.eclipse.paho.client.mqttv3.MqttClient behind MqttClientInterface. Used in production.

  • MqttConnectionConfig
    Data class grouping all six broker parameters (host, port, clientId, username, password, TLS flag). Includes a fromContext() factory that reads values from SettingsFragment. Keeps connect() to a single overload with one parameter.

  • MqttManager
    Core manager class:

    • Singleton via by lazy — follows the same pattern as RaspberryPiService
    • connect(config) — establishes broker connection, idempotent if already connected, validates host, auto-generates client ID if blank
    • connectFromContext(context) — convenience entry point reading config from SettingsFragment
    • disconnect() — graceful disconnect and resource cleanup, idempotent if not connected
    • isConnected — queryable connection state backed by the client
    • buildServerUri() — produces tcp:// or ssl:// URI based on TLS flag
    • buildConnectOptions() — sets clean session, timeout, and optional credentials
    • On connect failure: logs error, calls close() on the client to release any allocated resources, nulls the reference — app never crashes

Testing

Added MqttManagerTest with 26 unit tests using FakeMqttClient (hand-written test double — no Mockito, no emulator, pure JVM):

  • Singleton: instance is non-null and returns same object on repeated access
  • Initial state: isConnected is false before any call
  • connect() success path: returns true, calls client, sets callback
  • connect() failure path: returns false without crashing when MqttException thrown, calls close() for resource cleanup
  • connect() guard paths: already connected returns true without reconnecting; blank host returns false without touching client; empty client ID auto-generates one
  • URI building: tcp:// vs ssl:// scheme selection, full URI format
  • Connect options: isCleanSession always true, credentials set only when non-blank
  • disconnect(): calls disconnect() and close() on client; skips both when not connected; safe to call twice

Followed TDD workflow:

  • Red
  • Green
  • Refactor

26 unit tests added.

Static Analysis

Tool Result
Detekt ✅ Zero findings in MQTT files
Checkstyle ✅ Zero violations in MQTT files
Unit tests ✅ Full suite passes, no regressions

Your Checklist

  • Include the name of the Jira ticket in the PR's title
  • Include a summary of the changes plus the relevant context
  • Choose the proper base branch (develop)
  • Confirm that the changes follow the project's coding guidelines
  • Verify that the changes generate no compiler or linter warnings
  • Perform a self-review of the changes
  • Verify to commit no other files than the intentionally changed ones
  • Include reasonable and readable tests verifying the added or changed behavior
  • Confirm that new and existing unit tests pass locally
  • Check that the commits' message style matches the project's guideline
  • Stick to the project's gitflow workflow
  • Verify that your changes do not have any conflicts with the base branch
  • After the PR, verify that all CI checks have passed
  • Post a message in the catroid-stage or catroid-ide Slack channel and ask for a code reviewer

Add the Eclipse Paho MQTT Java library dependency to the Catroid
module's build configuration. This lays the groundwork for the
connection manager and the new MQTT bricks.

- Declare org.eclipse.paho.client.mqttv3:1.2.5 via Maven Central
  (Eclipse Paho repo not required)
- Declare org.eclipse.paho.android.service:1.1.1 via Maven Central
- Add FEATURE_MQTT_ENABLED build config flag in build.gradle
- Register MqttService in AndroidManifest.xml
- Add MqttDependencySanityTest to verify Paho classes are
  accessible on the test classpath

INTERNET and ACCESS_NETWORK_STATE permissions were already present
in the manifest — no changes required.
…d default host, disable MQTT settings if feature is not enabled
- Add MqttClientInterface to decouple MqttManager from Paho
- Add PahoMqttClient as thin production wrapper around MqttClient
- Add MqttConnectionConfig to group broker connection parameters
- Add MqttManager with connect/disconnect, connection state tracking,
  URI and options building, and graceful failure handling with logging
- Close client on connect failure to prevent resource leaks
- Fix getMqttHost fallback default to 192.168.0.1 (regression in
  cherry-picked dependency commit)
- Add unit tests covering full connection lifecycle and state

Depends on: mqtt-settings-entry-task-tdd (CATROID-1670, CATROID-1672)
This PR should only be merged after that PR is merged and rebased.
@sonarqubecloud

Copy link
Copy Markdown

@harshsomankar123-tech harshsomankar123-tech added the GSoC-2026 This ticket is assigned to the GSoC contributor. label Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GSoC-2026 This ticket is assigned to the GSoC contributor.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants